package br.com.android.financas;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import br.com.android.financas.activities.ConfiguracaoPoupanca;

public class WebServicePoupanca {
	private  final String SOAP_ACTION = "";
    private  final String METHOD_NAME = "getUltimoValorVO";
    private  final String NAMESPACE = "https://www3.bcb.gov.br/wssgs/services/FachadaWSSGS";
    private  final String URL = "https://www3.bcb.gov.br/wssgs/services/FachadaWSSGS";
    private long servico=196;
	private SoapObject request;
	private float percentual;
	private ProgressDialog progress;

	public WebServicePoupanca(Context context,boolean show) {
		new LoadWebservice(context,show).execute();
	}

	public float getPercentual() {
		return percentual;
	}

	private class LoadWebservice extends AsyncTask<String, String, Boolean> {

		Context context;
		boolean show;
		public LoadWebservice(Context context, boolean show) {
			this.context=context;
			this.show=show;
		}

		  @Override
	      protected void onPreExecute() {
			  if(show){
				  progress = new ProgressDialog(context);
		          //Cria novo um ProgressDialogo e exibe
		          progress.setMessage(context.getString(R.string.webservice));
		          progress.setCancelable(false);
		          progress.show();
			  }
	      }

		  @Override
	      protected void onPostExecute(Boolean result) {
			  if(show){
				  progress.dismiss();
			  }
	      }

		  @Override
	      protected void onProgressUpdate(String... values) {
	          //Atualiza mensagem
	    	  if(progress!=null)
	    		  progress.setMessage(values[0]);
	      }

		@Override
		protected Boolean doInBackground(String... params) {

			request=new SoapObject(NAMESPACE, METHOD_NAME);
			request.addProperty("in0",servico );

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        envelope.dotNet = true;
	        envelope.setOutputSoapObject(request);
	        try {
	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	            androidHttpTransport.call(SOAP_ACTION, envelope);
	            SoapObject result= (SoapObject) envelope.getResponse();
	            SoapObject valor = (SoapObject) result.getProperty("ultimoValor");
	            percentual=Float.parseFloat(valor.getProperty("svalor").toString());
				publishProgress(context.getString(R.string.webservice_indice)+" "+Float.toString(percentual));

				if(show){
					SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(context);

				   ((ConfiguracaoPoupanca)context).updateTaxa(percentual);
				}
				wait(3000);
	            return true;
	        } catch (Exception e) {
	        	publishProgress(context.getString(R.string.webservice_erro));
	        }
	        return false;

		}

	}
}
